package org.korecky.gnotedigitalorganizer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.korecky.gnotedigitalorganizer.svg.DntToSvg;
import org.w3c.dom.svg.SVGDocument;

public class DntFilePreviewWindow extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JSVGCanvas svgCanvas;

    /**
     * Create the dialog.
     * 
     * @throws IOException
     */
    public DntFilePreviewWindow(File dntFile) throws IOException {
	initComponents();
	// DNT to SVG
	DntToSvg dntToSvg = new DntToSvg();
	String svgContent = dntToSvg.Convert(dntFile.getAbsolutePath());
	// Create an SVG document.
	StringReader reader = new StringReader(svgContent);
	String parser = XMLResourceDescriptor.getXMLParserClassName();
	SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
	SVGDocument doc = f.createSVGDocument(null, reader);

	this.svgCanvas.setSVGDocument(doc);
    }

    private void initComponents() {
	setTitle("DNT preview");
	setIconImage(Toolkit.getDefaultToolkit().getImage(DntFilePreviewWindow.class.getResource("/org/korecky/gnotedigitalorganizer/resources/icons128x128/edit-find.png")));
	setBounds(100, 100, 433, 380);
	getContentPane().setLayout(new BorderLayout());
	contentPanel.setBackground(Color.WHITE);
	this.contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
	getContentPane().add(this.contentPanel, BorderLayout.CENTER);
	contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));

	this.svgCanvas = new JSVGCanvas();
	contentPanel.add(this.svgCanvas);
    }

}
